<?php

use app\admin\service\AgentRefund;
use think\facade\Db;
use think\migration\Migrator;
use think\migration\db\Column;

class FixAgentBlance extends Migrator
{
    public function up()
    {

        try {
            Db::startTrans();
            $agentRefund = \app\model\AgentRefund::where('id', 50)
                ->where('mobile', '18508175792')
                ->find();
            if (!$agentRefund) {
                echo '不存在id:18607542992' . PHP_EOL;
                return true;
            }

            $log   = \app\model\AgentBalanceLog::where('quote_id',$agentRefund->id)
                ->where('type',BALANCE_REFUND_CANCEL)
                ->find();

            if ($log){
                echo '已存在'.PHP_EOL;
                return true;
            }

            $agentRefund->state = 1;
            $agentRefund->user_remark = '系统调整退款';
            $agentRefund->save();

            $res = (new AgentRefund)->cancelByBalanceLogId($agentRefund->agent_id, $agentRefund->id);

            if ($res['code']){
                throw new \think\Exception('错误');
            }

            Db::commit();
        }catch (Exception $exception){
            Db::rollback();
            echo '失败';
        }

    }

    public function down()
    {

    }
}
